package com.mybatistest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatistest.entity.Mybatis;
import com.mybatistest.mapper.MybatisMapper;
import com.mybatistest.service.IMybatisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangrunfu
 * @since 2024-08-30
 */
@Service
@Slf4j
public class MybatisServiceImpl extends ServiceImpl<MybatisMapper, Mybatis> implements IMybatisService {


    @Resource
    MybatisMapper mybatisMapper;

    //增加数据
    @Override
    public boolean saveData(Mybatis mybatis) {
        return this.save(mybatis);
    }

    //分页查询
    @Override
    public List<Mybatis> getPage(int pageNum, int pageSize) {
        IPage<Mybatis> page = new Page<>(pageNum,pageSize);
        IPage<Mybatis> mybatisIPage = this.page(page);
        List<Mybatis> records = mybatisIPage.getRecords();
        long total = mybatisIPage.getTotal();
        log.info("总条数:{}",total);
        log.info("数据:{}",records);
        return records;
    }

    @Override
    public Mybatis queryByName(String citizen) {
        //return mybatisMapper.queryByName(name);
        log.info("调用数据库查询操作");
        QueryWrapper<Mybatis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("citizen",citizen);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Mybatis> queryByNameList(String name) {
        QueryWrapper<Mybatis> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        return this.list(queryWrapper);
    }

    @Override
    public List<Mybatis> queryByCitizenList(String citizen) {
        return lambdaQuery().like(Mybatis::getCitizen,citizen).list();
    }
}
